package com.dragee.modules.officialWebsite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragee.common.PageUtils;
import com.dragee.constants.CommonConstants;
import com.dragee.modules.officialWebsite.dao.PhotoAlbumDao;
import com.dragee.modules.officialWebsite.entity.PhotoAlbumDetailEntity;
import com.dragee.modules.officialWebsite.entity.PhotoAlbumEntity;
import com.dragee.modules.officialWebsite.service.PhotoAlbumDetailService;
import com.dragee.modules.officialWebsite.service.PhotoAlbumService;
import com.dragee.utils.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("photoAlbumService")
public class PhotoAlbumServiceImpl extends ServiceImpl<PhotoAlbumDao, PhotoAlbumEntity> implements PhotoAlbumService {


    @Resource
    private PhotoAlbumDao photoAlbumDao;
    @Autowired
    private PhotoAlbumDetailService photoAlbumDetailService;

    private String business = "photoAlbum";
    /**
    * 列表
    */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int limit = params.get(CommonConstants.LIMIT) == null
                ?CommonConstants.LIMIT_SIZE:Integer.parseInt(params.get(CommonConstants.LIMIT).toString());
        int start = params.get(CommonConstants.START) == null
                ?CommonConstants.START_SIZE:Integer.parseInt(params.get(CommonConstants.START).toString());
        QueryWrapper<PhotoAlbumEntity> wrapper =
                WrapperUtil.getWrapper(business, params, PhotoAlbumEntity.class);
        IPage<PhotoAlbumEntity> page = new Page<>(start, limit);
        page = baseMapper.selectPage(page, wrapper.select());
        for (PhotoAlbumEntity record : page.getRecords()) {
            List<PhotoAlbumDetailEntity> details = photoAlbumDetailService.getByPhotoId(record.getId().toString());
            if (details.size() > 0) {
                record.setFilePath(details.get(0).getFilePath());
                record.setFileName(details.get(0).getFileName());
            }
        }
        return new PageUtils(page);
    }



}